using System.Windows;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using Tp.Dto;
using Tp.Dto.Allgemein;
using Tp.Dto.Enums;
using Tp.Dto.EventArgs;
using Tp.Dto.Stammdaten;
using Tp.Interfaces;
using Tp.Interfaces.Commands;
using Tp.Interfaces.ViewModels;

namespace Tp.MainViewModel {
    public class MainWindowViewModel : ViewModelBase, IMainWindowViewModel {
        #region Fields

        private FrameworkElement _currentView;
        private EmployeeDto _dialogEmployee;
		private OrganisationDto organisation;
        private bool _isDialogVisible;
		private bool _isOrganisationDialogVisible;
		private bool isInProcess = false;

        #endregion

        #region Properties

        public IMainViewModelCommands Commands { get; set; }

        public FrameworkElement CurrentView {
            get {
                return this._currentView;
            }
            set {
                if (this._currentView != null && this._currentView.Equals(value)) {
                    return;
                }

                this._currentView = value;
                RaisePropertyChanged(() => CurrentView);
            }
        }

		public bool IsDialogVisible {
            get { return this._isDialogVisible; }
            set {
                if (this._isDialogVisible == value) {
                    return;
                }

                this._isDialogVisible = value;
                RaisePropertyChanged(() => IsDialogVisible);
            }
        }

		public IAbstractDto MyModel
		{
			  get;
			  set;
		}

        public EmployeeDto DialogEmployee {
            get { return this._dialogEmployee; }
            set {
                if (this._dialogEmployee == value) {
                    return;
                }

                this._dialogEmployee = value;
                RaisePropertyChanged(() => DialogEmployee);
            }
        }

        #endregion

        #region C-Tor

        public MainWindowViewModel(IMainViewModelCommands commands) {
            this.Commands = commands;
            this.RegisterMessengers();
        }

        #endregion

        #region Messengers

        private void RegisterMessengers() {

            Messenger.Default.Register<FrameworkElement>(this, ChangeView);
            Messenger.Default.Register<EmployeeDto>(this, ShowEmployeeDialog);
			Messenger.Default.Register<string>(this, DoJob);

        }

        #endregion

        #region Private methods

        private void ChangeView(FrameworkElement view) {

            // nicht immer senden!!!
            if (this.CurrentView == null || this.CurrentView.Name != view.Name) {
                switch (view.Name) {
                    case "MitarbeiterView":
                        Messenger.Default.Send(ProjectsOperationEnum.OperationsState.LoadEmployee);
                        break;
                    case "ProjectsView":
                        Messenger.Default.Send(new GeschaeftsfallOperationEventArgs { OperationName = "CheckBusiness" });
                        break;
                    case "OrganisationView":
						Messenger.Default.Send(OrganisationOperationEnum.OperationsState.LoadOrganisations);
						break;
					case "PersonenView":
						Messenger.Default.Send(new PersonenOperationsEventArgs { OperationName = "LoadPersons" });
						break;
                }
				  
                this.CurrentView = view;
            }
        }

		

        private void ShowEmployeeDialog(EmployeeDto employee) {
            this.IsDialogVisible = true;
            this.DialogEmployee = employee;
        }

		private void DoJob(string job) {
            switch (job.ToLowerInvariant()) {
                case "closedialog":
                    this.IsDialogVisible = false;
					break;
                default:
                    break;
            }
        }

        #endregion

        #region Cleaning
        public void CleanUp() {
            base.Cleanup();
        }
        #endregion
    }
}